module ApplicationHelper
  
  #被访问: :browser_version
  def accessed_by?(args = {})
    request.env['HTTP_USER_AGENT'].downcase.index(args[:browser_version])
  end
  
  # qtip args :content :tip :target :tooltip
  def qtip(jquery_selector,args = {})
    raw "$('#{jquery_selector}').qtip({
     content: '#{args[:content]}',
     style: {
       name : 'cream',
       tip: '#{args[:tip]}',
       border: {
         radius: 5
       }
     },           
     position: {
       corner: {
         target: '#{args[:target]}',
         tooltip: '#{args[:tooltip]}'
       }
     },
     show: {
       ready : true
     }
  })"
  end

  # 字符串截取
  # src：要截取的字符串
  # start：起始位置（可从0开始）
  # length：截取的长度
  def cut_string(src,start,length)
    require "iconv"
    @conv=Iconv.new("UTF-16","UTF-8")
    @reverse_conv=Iconv.new("UTF-8","UTF-16")
    p_start=start.class==Fixnum&&start>=0
    p_length=length.class==Fixnum&&length>=0
    return "" unless src&&p_start&&p_length
    src_utf16=@conv.iconv(src)
    cutted_src_utf_16=src_utf16[2*start+2,2*length]
    @reverse_conv.iconv(cutted_src_utf_16)
  end

  def loading_image_tag( size = :normal, options = {})
    options[:title] ||= '读取中...'
    options[:alt] ||= options[:title]
    case size
    when :large
      options[:class] ||= 'loading large'
      image_tag 'loading/54x18.gif', options
    when :normal
      options[:class] ||= 'loading normal'
      image_tag 'loading/36x12.gif', options
    when :small
      options[:class] ||= 'loading small'
      image_tag 'loading/24x8.gif', options
    else
      raise "未知尺码：#{size}"
    end
  end

end
